import { defineStore } from 'pinia'

export const useCounterStore = defineStore('counter', {
  persist: true,
  state: () => ({
    //用户id字段
    userId: 0,
    //用户名字段
    username: '',
    //头像
    avatar: '',
    //equipmentId字段
    equipmentId: 0,
    //持久化储存token
    token: '',
    //卖家id
    userId2: 0,
    //预定id
    reservationId:0,
    //订单id
    orderId:0,
    //一级分类
    typeId:'',
    //最新上架
    newGoods: '',
    //求购列表
    purchaseData:{}
    ,
    //验证码key
    captchaKey:''
  }),
  actions: {
      //监听器
       // 更新用户信息（核心方法：同步所有用户相关字段）
    updateUserInfo(info) {
      this.userId = info.userId || this.userId
      this.username = info.userName || this.username // 统一显示用字段
      this.userName = info.userName || this.userName
      this.avatar = info.avatar || this.avatar
      this.phone = info.phone || this.phone
      this.idCard = info.idCard || this.idCard
      this.userType = info.userType || this.userType
    },
    // 清空用户信息（退出登录用）
    clearUserInfo() {
      this.userId = 0
      this.username = ''
      this.userName = ''
      this.avatar = ''
      this.phone = ''
      this.idCard = ''
      this.userType = 0
      this.token = ''
    }
  }
})